<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryBaran">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleEdit()">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="brands" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>

      <el-table-column prop="name" label="品牌名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="firstChar" label="品牌首字母" width="160" sortable>
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length===0" @click="batchRemove()">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editBrandVisible" :close-on-click-modal="false">
      <el-form :model="editBrand" label-width="80px" :rules="editBrandRules" ref="editBrand">

        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="editBrand.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="首字母" prop="firstChar">
          <el-input v-model="editBrand.firstChar" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editBrandVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      brands: [],
      editBrand: [],
      total: 0,
      pageNum: 1,
      pageSize:5,
      listLoading: false,
      sels: [],//列表选中列

      editBrandVisible: false,//编辑界面是否显示
      editLoading: false,
      editBrandRules: {
        name: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' }
        ],
        firstChar: [
          { required: true, message: '请输入品牌首字母', trigger: 'blur' }
        ]
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

    }
  },
  methods: {

    selsChange: function (sels) {
      this.sels = sels;
    },

    handleCurrentChange(val) {
      this.pageNum = val;
      this.queryBaran();
    },
    //获取品牌列表
    queryBaran(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum,   //页码
        name:this.filters.keyword,
        firstChar:null
      }
      this.$http.post("/system/api/brand/queryPage",params)
          .then(resp=>{
              this.brands = resp.data.data.list;
              this.total = resp.data.data.total;
          }).catch(resp=>{
              this.$message({
                message: "请登录",
                type: 'error'
              });
      })
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.editBrandVisible = true;
      this.editBrand = Object.assign({}, row);
    },

    //编辑
    editSubmit: function () {
      this.$refs.editBrand.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editBrand);
            this.$http.put("/system/api/brand/save",para)
                .then(resp=>{
                  if (resp.data.code==200){
                      this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                      this.editLoading=false;
                      this.editBrandVisible=false;
                      this.queryBaran();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                    this.editLoading=false;
                    this.editBrandVisible=false;
                  }
                }).catch(resp=>{
                    this.$message({
                      message: "网络异常，稍后重试",
                      type: 'error'
                    });
                })
          });
        }
      });
    },


    //删除
   handleDel: function (index, row) {
     this.$confirm('确认删除该记录吗?', '温馨提示', {
       type: 'warning'
     }).then(() => {
       this.listLoading = true;
       let url = "/system/api/brand/delete/"+row.id;
       this.$http.delete(url)
           .then(resp=>{
                 //去除忙等框
              this.listLoading = false;
              if (resp.data.code==200){
               this.$message({
                 message: resp.data.msg,
                 type: 'success'
               });
                 this.queryBaran();
                 }else {
                   this.$message({
                     message: resp.data.msg,
                     type: 'error'
                   });
                 }
               }
           )
           .catch(result=>{
             //去除忙等框
             this.listLoading = false;
             //提示
             this.$message({
               message: '系统错误！',
               type: 'error'
             });
           })

     }).catch(() => {

     });
   },

       //批量删除
       batchRemove: function () {
         var ids = this.sels.map(item => item.id);
         this.$confirm('确认删除选中记录吗？', '提示', {
           type: 'warning'
         }).then(() => {
           this.listLoading = true;
           //NProgress.start();
           let para = {ids: ids };
           this.$http.patch("/system/api/brand/batchRemove",ids)
               .then(resp=>{
                     //去除忙等框
                     this.listLoading = false;
                     if (resp.data.code==200){
                       this.$message({
                         message: resp.data.msg,
                         type: 'success'
                       });
                       this.queryBaran();
                     }else {
                       this.$message({
                         message: resp.data.msg,
                         type: 'error'
                       });
                       this.listLoading = false;
                     }
                   }
               )
         }).catch(() => {
           this.listLoading = false;
           //提示
           this.$message({
             message: '系统错误！',
             type: 'error'
           });
         })
       }
  },
  mounted() {
    this.queryBaran();
  }
}

</script>

<style scoped>

</style>